package cn.com.yinhex.as.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import ayou.util.DateUtil;
import ayou.util.IntUtil;
import ayou.util.StringUtil;

public class DOC extends HashMap implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DateUtil du = null;

	public DOC() {
		du = new DateUtil();
	}

	public DOC(int size) {
		super(size);
		du = new DateUtil();
	}

	/**
	 * 得到字符串
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		if (!StringUtil.isFine(key)) {
			return "";
		}
		if (key.indexOf(",") > -1) {
			return this.multiGet(key.split(","));
		}
		String output = (String) super.get(key);
		if (output == null || "null".equals(output)) {
			return "";
		}
		return output.trim();
	}

	/**
	 * 得到Object值
	 * 
	 * @param key
	 * @return
	 */
	public Object geto(String key) {
		if (this == null) {
			return null;
		}
		return super.get(key);
	}

	/**
	 * 得到数字值
	 * 
	 * @param key
	 * @return
	 */
	public int geti(String key) {
		if (this == null) {
			return 0;
		}
		if (this.get(key) == null) {
			return 0;
		}
		String strkey = (String) this.get(key);
		int rtn = 0;
		try {
			rtn = Integer.parseInt(strkey);
		} catch (Exception ex) {
		}
		return rtn;
	}
	
	
	/**
	 * 得到数字值
	 * 
	 * @param key
	 * @return
	 */
	public int geti(String key,int def) {
		if (this == null) {
			return def;
		}
		if (this.get(key) == null) {
			return def;
		}
		String strkey = (String) this.get(key);
		int rtn = 0;
		try {
			rtn = Integer.parseInt(strkey);
		} catch (Exception ex) {
			return def;
		}
		return rtn;
	}

	/**
	 * 得到数字值
	 * 
	 * @param key
	 * @return
	 */
	public char getc(String key) {
		if (this == null) {
			return 0;
		}
		if (this.get(key) == null) {
			return 0;
		}
		String strkey = (String) this.get(key);
		return strkey.charAt(0);
	}

	public long getl(String key) {
		if (this == null) {
			return 0;
		}
		if (this.get(key) == null) {
			return 0;
		}
		String strkey = (String) this.get(key);
		long rtn = 0;
		try {
			rtn = Long.parseLong(strkey);
		} catch (Exception ex) {
		}
		return rtn;
	}

	public boolean getb(String key) {
		if (this == null) {
			return false;
		}
		if (this.get(key) == null) {
			return false;
		}
		String strkey = (String) this.get(key);
		if (strkey != null && (strkey.equals("1") || strkey.equals("y") || strkey.equals("yes") || strkey.equals("on") || strkey.equals("true"))) {
			return true;
		}
		return false;
	}

	public double getd(String key) {
		if (this == null) {
			return 0;
		}
		if (this.get(key) == null) {
			return 0;
		}
		String strkey = (String) this.get(key);
		double rtn = 0;
		try {
			rtn = Double.parseDouble(strkey);
		} catch (Exception ex) {
		}
		return rtn;
	}

	/**
	 * 添加一条记录
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public void put(String key, String value) {
		if (key == null) {
			return;
		}
		super.put(key, value);
	}

	/**
	 * 添加数字记录
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public void puti(String key, int value) {
		if (key == null) {
			return;
		}
		super.put(key, String.valueOf(value));
	}

	/**
	 * 得到doc的所有key
	 * 
	 * @return
	 */
	public String[] getKeys() {
		if (this == null) {
			return null;
		}
		Iterator it = this.iterator();
		String[] strs = new String[this.size()];
		int i = 0;
		while (it.hasNext()) {
			strs[i] = (String) it.next();
			i++;
		}
		return strs;
	}

	public Iterator iterator() {
		Set set = this.keySet();
		return set.iterator();
	}

	/**
	 * 得到doc的key的数量
	 * 
	 * @return
	 */
	public int getSize() {
		try {
			return this.size();
		} catch (Exception e) {
			return 0;
		}
	}

	public String getDate(String key) {
		String value = this.get(key);
		try {
			if (value.indexOf(",") > -1) {
				DateUtil du = new DateUtil();
				du.set(DateUtil.toLongTime(value));
				String date = du.getCurTime();
				if (date.indexOf("0000") != 0) {
					return du.getCurTime();
				}
			} else if (value.length() == 19) {
				return value;
			} else if (value.length() == 13) {
				DateUtil du = new DateUtil();
				du.set(IntUtil.s2l(value));
				return du.getCurTime();
			} else if (value.equals("0")) {
				return "";
			}
			DateUtil du = new DateUtil();
			return du.getCurTime().substring(0, 13) + ":00:00";
		} catch (Exception e) {
			DateUtil du = new DateUtil();
			return du.getCurTime().substring(0, 13) + ":00:00";
		}
	}

	public String multiGet(String[] strs) {
		if (strs == null) {
			return "";
		}
		String buffer = "";
		for (int i = 0; i < strs.length; i++) {
			buffer = this.get(strs[i]);
			if (StringUtil.isFine(buffer)) {
				return buffer;
			}
		}
		return buffer;
	}

	public long getLongTime(String key) {
		String value = this.get(key);
		try {
			if (value.indexOf("-") > -1) {
				DateUtil du = new DateUtil();
				du.set(value);
				return du.getLongtime();
			}
			return 0L;
		} catch (Exception e) {
			return 0L;
		}
	}
}
